<%@ page contentType="application/json;charset=UTF-8" language="java" %><%--
--%><%@ page import="spacecrack.Game" %><%--
--%><%@ page import="spacecrack.GameManager" %><%--
--%><%@ page import="spacecrack.NameRandomizer" %><%--
--%><%@ page import="spacecrack.Player" %><%--
--%><%@ page import="spacecrack.PlayerManager" %><%--
--%><%@ page import="java.util.List" %><%--
--%><%@ page import="o.json.JSONFactory" %><%--
--%><%@ page import="o.json.JSONWriter" %><%--
--%><%

	PlayerManager pm = new PlayerManager();

//	public Player getPlayer(String name) {
//		if (!pm.playerExists(name)) {
//			String id    = NameRandomizer.get(5) +"@test.com";
//			String name  = NameRandomizer.get(10);
//
//			pm.storePlayer(new Player(id, name));
//		}
//	}

	String id     = NameRandomizer.get(10);
	String name   = request.getParameter("name");
	String owner  = request.getParameter("user1");
	String status = "Starting";
	String user1  = request.getParameter("user1");
	String user2  = request.getParameter("user2");
	String user3  = request.getParameter("user3");
	String user4  = request.getParameter("user4");
	String user5  = request.getParameter("user5");
	String user6  = request.getParameter("user6");
	String user7  = request.getParameter("user7");
	String user8  = request.getParameter("user8");

	Game game = new Game(id, name, Player.getKey(owner), status);
	if (!user1.isEmpty()) game.addPlayerKey(Player.getKey(user1));
	if (!user2.isEmpty()) game.addPlayerKey(Player.getKey(user2));
	if (!user3.isEmpty()) game.addPlayerKey(Player.getKey(user3));
	if (!user4.isEmpty()) game.addPlayerKey(Player.getKey(user4));
	if (!user5.isEmpty()) game.addPlayerKey(Player.getKey(user5));
	if (!user6.isEmpty()) game.addPlayerKey(Player.getKey(user6));
	if (!user7.isEmpty()) game.addPlayerKey(Player.getKey(user7));
	if (!user8.isEmpty()) game.addPlayerKey(Player.getKey(user8));

	final GameManager gm = new GameManager();
	gm.storeGame(game);

	final List<Game> games = gm.loadGamesWithPlayers();

	final JSONWriter writer = new JSONWriter();
	final JSONFactory f = new JSONFactory();
	writer.write(f.create(games));

%><%= writer.toString() %>
